package com.runen.infinitemovement.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.runen.infinitemovement.Constant;
import com.runen.infinitemovement.R;
import com.runen.infinitemovement.base.BaseActivity;
import com.runen.infinitemovement.model.api.UserAPI;
import com.runen.infinitemovement.model.base.BaseResponseEntity;
import com.runen.infinitemovement.model.entity.EventBusEntity;
import com.runen.infinitemovement.tool.AppUtils;
import com.runen.infinitemovement.tool.CommonUtil;
import com.runen.rxnetlibrary.net.callback.BaseResultCallback;
import com.runen.rxnetlibrary.utils.ToastUtils;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AddTeamActivity extends BaseActivity {

    @BindView(R.id.edt_input_name)
    EditText edtInputName;
    @BindView(R.id.tv_check_team)
    TextView tvCheckTeam;
    @BindView(R.id.tv_send_login)
    TextView tvSendLogin;
    @BindView(R.id.tv_login)
    TextView tvLogin;
    @BindView(R.id.tv_long_discrible)
    TextView tvLongDiscrible;
    @BindView(R.id.v_line)
    View vLine;
    @BindView(R.id.rl_code)
    RelativeLayout rlCode;
    @BindView(R.id.v_line2)
    View vLine2;
    @BindView(R.id.ll_submit)
    RelativeLayout llSubmit;
    @BindView(R.id.ll_submit_check)
    LinearLayout llSubmitCheck;
    @BindView(R.id.ll_user_agreement)
    LinearLayout llUserAgreement;
    @BindView(R.id.tv_user_agreement)
    TextView tvUserAgreement;
    @BindView(R.id.tv_privacy_agreement)
    TextView tvPrivacyAgreement;
    private int edtSize;
    private int flagFinish;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_add_team;
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        ButterKnife.bind(this);
        int branchCheck = getIntent().getIntExtra("BranchCheck", 0);
        if (branchCheck == 1) {
            llSubmit.setVisibility(View.GONE);
            llSubmitCheck.setVisibility(View.VISIBLE);
        } else {
            llSubmit.setVisibility(View.VISIBLE);
            llSubmitCheck.setVisibility(View.GONE);
        }
        edtInputName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                edtSize = edtInputName.getText().toString().intern().length();
                String branchName = tvCheckTeam.getText().toString();
                if (edtSize < 2 || TextUtils.equals(branchName, "选择所在部门")) {
                    tvSendLogin.setBackground(CommonUtil.getDrawable(R.drawable.bg_fa5438_f63d5b_dp360_alpha));
                    tvSendLogin.setEnabled(false);
                } else {
                    tvSendLogin.setBackground(CommonUtil.getDrawable(R.drawable.bg_fa5438_f63d5b_dp360));
                    tvSendLogin.setEnabled(true);
                }
            }
        });
    }

    @Override
    protected boolean isUseFullScreenMode() {
        return true;
    }

    @OnClick({R.id.tv_check_team, R.id.tv_send_login, R.id.tv_user_agreement, R.id.tv_privacy_agreement})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_check_team:
                Intent intent2 = new Intent(this, CheckBranchActivity.class);
                startActivityForResult(intent2, 99);
                break;
            case R.id.tv_send_login:
                submit();
                break;
            case R.id.tv_user_agreement:
                Intent intent = new Intent(this, WebActivity.class);
                intent.putExtra("LINK_URL", Constant.USER_AGREEMENT);
                intent.putExtra("flag", 1);
                startActivity(intent);
                break;
            case R.id.tv_privacy_agreement:
                Intent intent3 = new Intent(this, WebActivity.class);
                intent3.putExtra("LINK_URL", Constant.PRIVACY_AGREEMENT);
                intent3.putExtra("flag", 2);
                startActivity(intent3);
                break;
        }
    }

    String tenantId;
    String topId;

    private void submit() {
        String realName = edtInputName.getText().toString().trim();
        if (TextUtils.isEmpty(realName)) {
            ToastUtils.showShort("请输入真实姓名");
            return;
        }
        if (TextUtils.isEmpty(tenantId)) {
            ToastUtils.showShort("请选择所在部门");
            return;
        }
        JSONObject object = new JSONObject();
        try {
            object.put("realName", realName);
            object.put("tenantId", tenantId);
            object.put("topTenantId", topId);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        new UserAPI().submitBranch(object.toString(), 0, new BaseResultCallback<BaseResponseEntity<?>>() {
            @Override
            public void onResponse(BaseResponseEntity<?> response) {
                if (response.getCode() == 0) {
                    llSubmit.setVisibility(View.GONE);
                    llSubmitCheck.setVisibility(View.VISIBLE);
                    EventBus.getDefault().post(new EventBusEntity(4));
                    flagFinish = 1;
                    AppUtils.jumpToMain(AddTeamActivity.this);
                } else {
                    ToastUtils.showShort(response.getMessage());
                    flagFinish = 0;
                }
            }
        });
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 99) {
            if (data != null) {
                String branchName = data.getStringExtra("BRANCH_NAME");
                String checkId = data.getStringExtra("BRANCH_ID");
                String topCheckId = data.getStringExtra("BRANCH_TOP_ID");
                topId = topCheckId;
                tenantId = checkId;
                if (!TextUtils.isEmpty(branchName)) {
                    tvCheckTeam.setText(branchName);
                    if (edtSize < 2 || TextUtils.equals(branchName, "选择所在部门")) {
                        tvSendLogin.setBackground(CommonUtil.getDrawable(R.drawable.bg_fa5438_f63d5b_dp360_alpha));
                        tvSendLogin.setEnabled(false);
                    } else {
                        tvSendLogin.setBackground(CommonUtil.getDrawable(R.drawable.bg_fa5438_f63d5b_dp360));
                        tvSendLogin.setEnabled(true);
                    }
                }
            }
        }
    }

    @Override
    public void onBackPressed() {
        if (flagFinish == 0) {
            UserAPI.clearLogin();
        }
        super.onBackPressed();
    }
}
